package com.tencent.crazypenguin.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
import com.tencent.crazypenguin.bean.Comment;
import com.tencent.crazypenguin.bean.MatchList;
import com.tencent.crazypenguin.bean.User;
import com.tencent.crazypenguin.config.HbmUtil;
import com.tencent.crazypenguin.config.ResultType;
import com.tencent.crazypenguin.dao.CommentDao;
import com.tencent.crazypenguin.dao.UserDao;
import com.tencent.crazypenguin.util.JPushUtil;
import com.tencent.crazypenguin.util.JsonUtil;

import cn.jpush.api.JPushClient;
import net.sf.json.JSONObject;

public class doMatch extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9184873986827257455L;
	
	private String result;

	@Override
	public String execute() throws Exception {
		JSONObject rspData = new JSONObject();
		try {
			JSONObject authData = HbmUtil.getAuthDate();
			UserDao userDao = new UserDao();
			long userId = authData.getLong("userId");
			String type = authData.getString("type");
			String message = authData.getString("message");
			Long targetId = authData.getLong("targetId");
			
			User targetUser = userDao.findById(targetId,new User());
			String devId = targetUser.getDev_id();
			
			JSONObject pushData = new JSONObject();
			pushData.put("message", message);
			pushData.put("fromId", authData.getString("userId"));
			pushData.put("type", type);
//			JPushUtil.one().sendToIOS(devId, pushData.toString());
			
			if(type.equals("ask")){
				MatchList.RemoveMuser(userId);
			}
			
			if(type.equals("answer") && message.equals("yes")){
				MatchList.RemoveMuser(userId);
				CommentDao cmDao = new CommentDao();
				long id_docom = authData.getLong("userId");
				long id_getcom = targetId;
				Date time_go = new Date();
				long star_com = 0;
				
				cmDao.save(new Comment(id_docom,id_getcom,time_go,star_com));
				cmDao.save(new Comment(id_getcom,id_docom,time_go,star_com));
			}
			
			rspData.put("resultCode", ResultType.SUCEESS);
			result = rspData.toString();
			return "done";
			
		} catch (Exception e) {
			e.printStackTrace();
			rspData.put("resultCode", ResultType.EXCEPTION);
			result = rspData.toString();
			return "done";
		}
		
		
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	

}
